package com.example.fixsys.controller;

import com.example.fixsys.common.Result;
import com.example.fixsys.entity.Reimburse;
import com.example.fixsys.entity.Users;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController("users")
public class UsersController {
    @Resource
    JdbcTemplate jdbcTemplate;
    @GetMapping("getUsers")
    public Result<?> GetUsers(){
        String sql = "select * from users";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }
    @PutMapping("/addUsers")
    public Result<?> AddUsers(@RequestBody Users users){
        String sql="insert into users(userNo,userAccount,userPassword,userRole,userName,userPhone,userIDCard,IDCardCheck) VALUES (?,?,?,?,?,?,?,?)";
        try{
            jdbcTemplate.update(sql, new Object[]{users.getUserNo(), users.getUserAccount(), users.getUserPassword(), users.getUserRole(), users.getUserName(), users.getUserPhone(), users.getUserIDCard(), users.getIDCardCheck()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/updateUsers")
    public Result<?> UpdateUsers(@RequestBody Users users){
        String sql="update users set userAccount=?,userPassword=?,userRole=?,userName=?,userPhone=?,userIDCard=?,IDCardCheck=? where userNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{users.getUserAccount(), users.getUserPassword(), users.getUserRole(), users.getUserName(), users.getUserPhone(), users.getUserIDCard(), users.getIDCardCheck(), users.getUserNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteUsers")
    public Result<?> DeleteUsers(@RequestBody Users users){
        String sql="delete from users where userNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{users.getUserNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }
}
